import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;


public class AffichageConsole {
	
	static Console console;
	
	public HashMap<String,Integer> argFctPredefinie = new HashMap<String,Integer>();
	
	public HashMap<String,Method> methodFctPredefinie = new HashMap<String,Method>();
	
	
	public AffichageConsole(Console console) {
		this.console=console;
		
		argFctPredefinie.put("ecris", 1);
		argFctPredefinie.put("error", 1);
		argFctPredefinie.put("system", 1);
		
		Class c = AffichageConsole.class;
		Method[] tab = c.getMethods();
		
		Method  m;
		for (int i=0;i<tab.length;i++) {
			m = tab[i];
			if(m.getName().equals("ecris"))
				methodFctPredefinie.put("ecris", m);						
			if(m.getName().equals("error"))
				methodFctPredefinie.put("error", m);
			if(m.getName().equals("system"))
				methodFctPredefinie.put("system", m);
		}    
	}
	
	public  static Object ecris(ArrayList<Object> v,  HashMap<String, Object> penv) throws ParseException, Exception{		
		if(v.size()==1) {
			if(v.get(0) instanceof ArrayList) 
				console.out(listToString((ArrayList)v.get(0)));
			else
				console.out((String) v.get(0));
			return true;
		}
		else 
			return false;
	}
	
	public  static Object error(ArrayList<Object> v,  HashMap<String, Object> penv) throws ParseException, Exception{		
		if(v.size()==1) {
			if(v.get(0) instanceof ArrayList) 
				console.err(listToString((ArrayList)v.get(0)));
			else
				console.err((String) v.get(0));
			return true;
		}
		else 
			return false;
	}
	
	public  static Object system(ArrayList<Object> v,  HashMap<String, Object> penv) throws ParseException, Exception{		
		if(v.size()==1) {
			if(v.get(0) instanceof ArrayList) 
				console.sys(listToString((ArrayList)v.get(0)));
			else
				console.sys((String) v.get(0));
			return true;
		}
		else 
			return false;
	}
	
	private static String listToString(ArrayList l){
		String list = "[ ";
		
		for(int i=0; i<l.size();i++){
			if(l.get(i) instanceof ArrayList)
				list += listToString((ArrayList)l.get(i))+" ";
			else
				list += l.get(i) +" ";
		}
		list += "]";
		return list;
	}

}
